<template>
	<view>
		<view class="recharge_way card_box">
			<view class="recharge_way_list">
				<view class="recharge_way_item">
					<view class="menu_item" @click="showBankList">
						<label class="radio">
							<view class="avatoa">
								<image src="../../../static/images/card.png" class="imgava"></image>
								<view class="">
									<view class="">
										<text class="menu_title">{{bankChange.bankName}}</text>
									</view>
									<view class="">
										<text class="bank_card_no">{{bankChange.bankCardNo}}</text>
									</view>
								</view>
							</view>
							<view class="">
								<text class="menu_title arrow_icon">〉</text>
							</view>
						</label>
					</view>
				</view>
			</view>
		</view>
		<view class="recharge_box card_box">
			<view class="recharge_hint">
				提现金额（收取{{rateNo*100}}%服务费）
			</view>
			<view class="recharge_input">
				￥<input class="input" type="digit" value="" v-model="amount" /><text class="input_btn" @click="withdrawAll">全部</text>
			</view>
			<view class="recharge_hint recharge_hint_lv2">
				账户余额： {{allAmount}}元
			</view>
		</view>
		<view class="submit">
			<view class="submitBtn" @click="rechargeBtn">预计两个小时到账，确认提现</view>
		</view>
		<Popup ref="showBankList" title="请选择银行" :full="true">
			<view class="bankList">
				<radio-group @change="changeBank">
					<view class="menu_item bottom_line" v-for="(bank,index) in bankCardLists" :key="index">
						<label class="radio">
							<view class="avatoa">
								<image src="../../../static/images/card.png" class="imgava"></image>
								<view class="">
									<view class="">
										<text class="menu_title">{{bank.bankName}}</text>
									</view>
									<view class="">
										<text class="bank_card_no">{{bank.bankCardNo}}</text>
									</view>
								</view>
							</view>
							<view class="">
								<radio :value="bank.bankCardNo" :checked="index === current" color="#195ED7" />
							</view>
						</label>
					</view>
				</radio-group>
			</view>
		</Popup>
	</view>
</template>

<script>
	import {regFenToYuan} from '../../../common/js/utils.js'
	import Popup from '@/components/popup/popup.vue'
	export default {
		data() {
			return {
				amount: '',
				balance: '500.40',
				bankList: [],
				current: 0,
				rateNo: 0,
				bankChange: {},
				allAmount: 0,
				bankCardLists: []
			};
		},
		components: {
			Popup
		},
		onShow:function(){
			this._queryBalance()
			this._getFree()
			this._queryBankCard()
		},
		methods: {
			_getFree() {
				uni.request({
					method: 'GET',
					url: 'https://qiancheng.mynatapp.cc/common/biz/cloud/order/fee/1',
					data: {
					},
					header: { 'content-type': 'application/json' },
					success: (res) => {
						console.log(res)
						if(res.data.code == 10000) {
							this.rateNo = res.data.data
						} else {
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			_queryBankCard() {
				uni.showLoading({
					title: '加载中...'
				})
				let bizUserId = uni.getStorageSync('bizUserId')
				uni.request({
					method: 'POST',
					url: 'https://wlreaohs.xiaomy.net/common/biz/cloud/member/queryBankCard',
					data: {
						bizUserId: bizUserId
					},
					header: { 'content-type': 'application/json' },
					success: (res) => {
						if(res.data.code == 0) {
							if(res.data.data.bindCardList == null) {
								this.bankCardLists = []
							} else {
								this.bankCardLists = res.data.data.bindCardList
								this.bankChange = this.bankCardLists[this.current]
							}
							uni.hideLoading()
						} else {
							uni.hideLoading()
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err)
					}
				})
			},
			_queryBalance() {
				let bizUserId = uni.getStorageSync('bizUserId')
				uni.request({
					method: 'POST',
					url: 'https://qiancheng.mynatapp.cc/common/biz/cloud/order/queryBalance',
					data: {
						bizUserId: bizUserId,
						accountSetNo: '400193'
					},
					header: { 'content-type': 'application/json' },
					success: (res) => {
						if(res.data.code == 10000) {
							uni.hideLoading()
							this.allAmount = regFenToYuan(res.data.data.allAmount)
						} else {
							uni.hideLoading()
						}
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err)
					}
				})
			},
			rechargeBtn() {
				if(this.amount == '') {
					uni.showToast({
						title: '请输入提现金额',
						icon: 'none'
					})
				} else {
					uni.showLoading({
						title: '提现中...'
					})
					let bizUserId = uni.getStorageSync('bizUserId')
					uni.request({
						method: 'POST',
						url: 'https://wlreaohs.xiaomy.net/common/biz/cloud/order/withdrawApply',
						data: {
							bizUserId: bizUserId,
							amount: this.amount,
							validateType: 0,
							bankCardNo: this.bankChange.bankCardNo,
							source: 1
						},
						header: { 'content-type': 'application/json' },
						success: (res) => {
							if(res.data.success == false) {
								uni.hideLoading()
								uni.showModal({
									title: '温馨提示',
									content: `${res.data.subMsg}`,
									showCancel: false
								});
							} else {
								uni.hideLoading()
								uni.showModal({
									title: '温馨提示',
									content: `提现成功~`,
									showCancel: false,
									success: function (res) {
										if (res.confirm) {
											uni.switchTab({
												url: '/pages/me/me'
											})
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							}
						},
						fail: (err) => {
							uni.hideLoading()
							console.log(err)
						}
					})
				}
			},
			showBankList() {
				this.$nextTick(() => {
					this.$refs.showBankList.open()
				})
			},
			changeBank(e) {
				this.bankChange = this.bankCardLists.filter((item,index) => {
					if(item.bankCardNo == e.target.value){
						this.current = index;
					}
					return item.bankCardNo == e.target.value;
				})[0]
				this.$nextTick(() => {
					this.$refs.showBankList.close()
				})
			},
			withdrawAll() {
				this.amount = this.allAmount
			},
		},
		computed:{}
	}
</script>
<style>
	page {
		background-color: #f8f8f8;
	}
</style>
<style lang="scss">
	.card_box {
		box-shadow: 0px 2px 8px 0px #ededed;
		margin-bottom: 20rpx;
		background-color: #FFF;
	}

	.recharge_hint {
		font-size: 34rpx;
		padding: 36rpx;

		&.recharge_hint_lv2 {
			font-size: 30rpx;
			color: #999;
		}
	}

	.recharge_input {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		font-size: 60rpx;
		padding: 0 36rpx;

		.input {
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			padding: 24rpx;
			font-size: 80rpx;
		}

		.input_btn {
			display: inline-block;
			width: 120rpx;
			color: #0A98D5;
			font-size: 30rpx;
		}
	}

	.recharge_way_list {
		margin-top: 24rpx;
	}

	.menu_item {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx;
		background-color: #FFFFFF;

		.radio {
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}

		&:active {
			background-color: #f6f6f6;
		}

		&.bottom_line {
			position: relative;

			&::after {
				content: '';
				display: block;
				position: absolute;
				bottom: 0;
				background-color: #eee;
				width: 100%;
				left: 100rpx;
				height: 1px;
				transform: scaleY(0.5);
			}

			// &::after,
			// &::before {
			// 	content: '';
			// 	position: absolute;
			// 	top: 0;
			// 	width: 24px;
			// 	height: 24px;
			// 	border-radius: 12px;
			// 	background-color: #F1F0F5;
			// }

			// &::before {
			// 	left: -40px;
			// }

			// &::after {
			// 	right: -40px;
			// }
		}
	}


	.avatoa {
		display: flex;
		align-items: center;
	}

	.imgava {
		width: 56rpx;
		height: 56rpx;
		margin-right: 25rpx;
	}

	.menu_title {
		font-family: PingFangSC;
		font-weight: 400;
		font-size: 34rpx;
		color: #333;
	}

	.bank_card_no {
		font-size: 28rpx;
		color: #999;
	}

	.arrow_icon {
		color: #333;
	}

	// 按钮组
	.submit {
		text-align: center;
		padding: 24upx;

		.submitBtn {
			border-width: 0px;
			height: 100upx;
			line-height: 100upx;
			background: inherit;
			background-color: #477EDA;
			box-sizing: border-box;
			border-width: 0;
			border-radius: 6upx;
			-moz-box-shadow: none;
			-webkit-box-shadow: none;
			box-shadow: none;
			color: #FFFFFF;

			&.radius_btn {
				border-radius: 50upx;
				box-shadow: 0px 8px 9px 0px #c3defc;
			}

			&:active {
				background-color: #3266a5;
			}

			&::after {
				border: none;
			}
		}
	}
</style>
